package cn.witsky.cdrService;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {
	private static Logger logger = LoggerFactory.getLogger(Test.class);	
	public static void main(String args[]) {
		int delay = 15;
		TimeUnit delayTimeUnit = TimeUnit.SECONDS;
		ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
		LocalDateTime date = LocalDateTime.now();
		int second = date.getSecond();
		int count=second/delay;
		int initialDelay = 0;
		if(second>count*delay) {
			initialDelay=(count+1)*delay-second;
		}
		logger.info("initialDelay:{} delay:{}",initialDelay,delay);
		executorService.scheduleAtFixedRate(new Runnable() {
			public void run() {
				try {
					logger.info("任务执行了.............");
					try {
						TimeUnit.SECONDS.sleep(2);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					int j=1/0;
				}catch(Exception ex) {
					logger.error("run error:",ex);
				}

			}
		}, initialDelay, delay, delayTimeUnit);
	}
}
